package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.HelpVideo;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.HelpVideoService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("sys/helpVideo")
@Api(tags = "骑遇新闻管理")
public class HelpVideoController {


    @Autowired
    private HelpVideoService helpVideoService;


    @ApiOperation(value = "分页查询新闻")
    @GetMapping
    public JsonResult<Page<HelpVideo>> findByPage(Page<HelpVideo> page) {
        Page<HelpVideo> videoIPage = helpVideoService.page(page);
        return JsonResult.ok(videoIPage);
    }


    @ApiOperation(value = "根据ID查询新闻")
    @GetMapping("{id}")
    public JsonResult<HelpVideo> findSysNews(@PathVariable Long id) {
        HelpVideo helpVideo = helpVideoService.getById(id);
        return JsonResult.ok(helpVideo);
    }


    @ApiOperation(value = "添加新闻")
    @PostMapping
    public JsonResult addSysNews(@RequestBody HelpVideo helpVideo) {
        boolean save = helpVideoService.save(initDB(helpVideo));
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改新闻")
    @PutMapping
    public JsonResult updateSysNews(@RequestBody HelpVideo helpVideo) {
        boolean update = helpVideoService.updateById(initDB(helpVideo));
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置新闻状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody HelpVideo helpVideo) {
        boolean save = helpVideoService.updateById(helpVideo);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除新闻")
    @DeleteMapping("{id}")
    public JsonResult findRepairMaint(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = helpVideoService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    private HelpVideo initDB(HelpVideo helpVideo) {
        if (helpVideo.getId() == null) {
            int count = helpVideoService.count(new LambdaQueryWrapper<HelpVideo>().eq(HelpVideo::getName, helpVideo.getName()));
            if (count > 0) {
                throw new MyException("帮助已近存在");
            }
            helpVideo.setId(IdWorker.getId());
            helpVideo.setCreated(new Date());
            helpVideo.setStatus((byte) 1);
            helpVideo.setCreateBy(SecurityUtils.getUserId());
            return helpVideo;
        }
        return helpVideo;
    }


}
